import 'package:logger/logger.dart';
import 'package:common_utils/common_utils.dart';

/// log 工具类
/// @author: dingwen
/// @date: 2020/12/28

///当前App的运行环境，product环境考验选择是否关闭日志打印
 bool _isProduct = bool.fromEnvironment("dart.vm.product");
class LogUtil {
  static Logger _logger;
  static Logger getInstance() {
    if (_logger == null) {
      _logger = Logger(
        printer: PrettyPrinter(methodCount: 0),
      );
    }
    if(_isProduct){
      _logger.close();
    }
    return _logger;
  }

  static String formatStr(
      {String fileName, String functionName, String message}) {
    return '【$fileName】\t（$functionName）\t$message \t${DateUtil.formatDate(DateTime.now(), format: DateFormats.full)}\t';
  }
}

///全局log打印
///后期可以考虑将日志输送服务器
log({Level level, String fileName, String functionName, String message}) {
  //生产环境取消日志打印
  if(_isProduct){
    return;
  }
  switch (level) {
    case Level.verbose:
      LogUtil.getInstance().v(LogUtil.formatStr(
          fileName: '$fileName',
          functionName: '$functionName',
          message: '$message'));
      break;
    case Level.info:
      LogUtil.getInstance().i(LogUtil.formatStr(
          fileName: '$fileName',
          functionName: '$functionName',
          message: '$message'));
      break;
    case Level.warning:
      LogUtil.getInstance().w(LogUtil.formatStr(
          fileName: '$fileName',
          functionName: '$functionName',
          message: '$message'));
      break;
    case Level.debug:
      LogUtil.getInstance().d(LogUtil.formatStr(
          fileName: '$fileName',
          functionName: '$functionName',
          message: '$message'));
      break;
    case Level.error:
      LogUtil.getInstance().e(LogUtil.formatStr(
          fileName: '$fileName',
          functionName: '$functionName',
          message: '$message'));
      break;
    case Level.wtf:
      LogUtil.getInstance().wtf(LogUtil.formatStr(
          fileName: '$fileName',
          functionName: '$functionName',
          message: '$message'));
      break;
  }
}
